
const fs = require('fs-extra')
const path = require('path');

const Router = require('koa-router')
//引入路由机制
let router = new Router();
/**
* 动态查找所有入口文件
*/
function GetFileNameArr(path) {
    let components = []
    const files = fs.readdirSync(path)
    //console.log(files, "========files==========");
    files.forEach(function (item, index) {
        let stat = fs.lstatSync(path + item)
        if (stat.isDirectory() === false) {
            components.push(item)
        }
    })
    return components;
}

//所有controller下的文件
var fileNameArr = GetFileNameArr('./controllers/')

console.log(fileNameArr, "=========folderNameArr=========");

fileNameArr.forEach(fileName => {

    fileNameWithOutExt = fileName.substring(0, fileName.lastIndexOf('.'))
    console.log(fileNameWithOutExt,"=========fileNameWithOutExt=========")

    if (fileNameWithOutExt != 'index') {
        const full_path = path.join(__dirname, `./${fileNameWithOutExt}/`);
        router.use(`/${fileNameWithOutExt}`, require(full_path))
        // router.use('/user', require('./controllers/user'))

    }
})

console.log(router, "=======router=======");

module.exports = function () {
    return router.routes()
}